package com.centaline.androidsalesblog.base;

import android.view.View;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.model.databaseobject.CityCenterDo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.orhanobut.logger.Logger;
import com.trello.rxlifecycle.FragmentEvent;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 列表地图模式
 * Created by guilin on 16/2/15.
 */
public abstract class BaseMapFragment extends BaseFragment implements BaiduMap.OnMapLoadedCallback,
        BaiduMap.OnMapStatusChangeListener {

    protected TextureMapView textureMapView;
    protected BaiduMap baiduMap;
    protected boolean setMapStatusLimits;//限制显示范围
    protected int cityCodeInt;
    protected float zoom;//最新的缩放级别
    protected LatLng centerTarget;//中心点坐标
    protected boolean fromUserClick;//用户控制

    @Override
    public void onResume() {
        textureMapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        textureMapView.onPause();
        super.onPause();
    }

    @Override
    public void onDestroy() {
        textureMapView.onDestroy();
        super.onDestroy();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_map;
    }

    @Override
    protected void findViews(View view) {
        textureMapView = (TextureMapView) view.findViewById(R.id.textureMapView);
    }

    @Override
    protected void initView() {
        baiduMap = textureMapView.getMap();
        UiSettings uiSettings = baiduMap.getUiSettings();
        uiSettings.setCompassEnabled(true);
        uiSettings.setOverlookingGesturesEnabled(false);
        uiSettings.setRotateGesturesEnabled(false);
        baiduMap.setMyLocationEnabled(true);
        baiduMap.setOnMapStatusChangeListener(this);
        baiduMap.setOnMapLoadedCallback(this);
        cityCodeInt = Integer.parseInt(AppConstant.CityCode);
        initOther();
    }

    /**
     * 初始化其他信息
     */
    protected abstract void initOther();

    @Override
    public void onMapLoaded() {
        textureMapView.showZoomControls(false);
        baiduMap.setMaxAndMinZoomLevel(20f, 9f);
        RxDataBase.cityCenterDo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<CityCenterDo>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<CityCenterDo>() {
                    @Override
                    public void call(CityCenterDo cityCenterDo) {
                        final LatLng latLng = new LatLng(cityCenterDo.getLat(), cityCenterDo.getLng());
                        baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngZoom(latLng, 9f));
                        setMapStatusLimits();
                    }
                });
    }

    private void setMapStatusLimits() {
        Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(2)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<Long>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        Logger.d("aLong : %d", aLong);
                        if (aLong == 1) {
                            setMapStatusLimits = true;
                            baiduMap.setMapStatusLimits(baiduMap.getMapStatus().bound);
                            initRegion();
                        }
                    }
                });
    }

    /**
     * 初始化区域数据
     */
    protected void initRegion() {

    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {
        Logger.d("onMapStatusChangeStart level : %f", mapStatus.zoom);
        zoom = mapStatus.zoom;
        centerTarget = mapStatus.target;
    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {
    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        Logger.d("onMapStatusChangeFinish level : %f", mapStatus.zoom);
    }

}
